#!/usr/bin/python
#
#  Copyright (c) 2008 Paul Banks [http://www.paulbanks.org/contact]
#
#  This file is free software: you may copy, redistribute and/or modify it  
#  under the terms of the GNU General Public License as published by the  
#  Free Software Foundation, either version 2 of the License, or (at your  
#  option) any later version.  
#  
#  This file is distributed in the hope that it will be useful, but  
#  WITHOUT ANY WARRANTY; without even the implied warranty of  
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
#  General Public License for more details.  
#  
#  You should have received a copy of the GNU General Public License  
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.  

import wx
import random
import hexicsolve

class HexicBoardMaker():

	tilecolours = [
		"GREY",
		"BLUE",
		"YELLOW",
		"RED",
		"GREEN",
		"PURPLE",
		"PINK",
		"DARK GREEN",
		"BLACK",
		"WHITE"
	]

	def __init__(self, xo, yo, size, bordersize):
		self.b = []
		self.br = []
		self.t = []
		self.tp = []
		self.xo = xo
		self.yo = yo
		self.s = size
		self.brushes = []
		for c in self.tilecolours:
			self.brushes.append(wx.Brush(c))

		self.border = bordersize

	def Func(self, x,y,o,p):
		if p.nValue > 0:
			cx = (x*(self.s-(self.s/4)+self.border))+(self.s/2)+self.xo
			cy = (y*(self.s+self.border))+self.s+self.yo+(o*self.s/2)-(self.s/2) 
			self.b.append( self.MakeHex(cx, cy, self.s ) )
			self.br.append(self.brushes[p.nValue])
			if (p.nRotate):
				self.tp.append((cx,cy))
				self.t.append("R%d" % p.nRotate)

	def MakeHex(self, x, y, s):
		h = []
		h.append( (x-(s/2),y) )
		h.append( (x-(s/2)+(s/4),y-(s/2)) )
		h.append( (x+(s/2)-(s/4),y-(s/2)) )
		h.append( (x+(s/2),y) )
		h.append( (x+(s/2)-(s/4),y+(s/2)) )
		h.append( (x-(s/2)+(s/4),y+(s/2)) )
		h.append( (x-(s/2),y) )
		return h

	def Draw(self, dc):
		dc.DrawPolygonList(self.b,None,self.br)
		dc.DrawTextList(self.t, self.tp)

class HexicPanel(wx.Panel):

	def __init__(self, parent, log, brd):
		self.log = log
		wx.Panel.__init__(self, parent, -1)
		self.brd = brd
		self.Bind(wx.EVT_PAINT, self.OnPaint)
		self.SetSize((5+(10*(40+2)), 5+(9*(40+2))))

	def BoardIter(self, boarditerer):
		for x in range(0, 10):
			if (x%2==0):
				for y in range(0,8):
					boarditerer.Func(x,y,1,self.brd.p(x,y));
			else:
				for y in range(0,9):
					boarditerer.Func(x,y,0,self.brd.p(x,y));
					
	def OnPaint(self, evt):
		dc = wx.PaintDC(self)

		# Draw board
		bmaker = HexicBoardMaker(5, 5, 40, 2)
		self.BoardIter(bmaker)
		bmaker.Draw(dc)


class HexicSolverFrame(wx.Frame):
	def __init__(self, parent, title, brd, pos=wx.DefaultPosition,
            size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE
         	):
		
		wx.Frame.__init__(self, parent, -1, title, pos, size, style)
		self.brd = brd
		
		panel = wx.Panel(self)
		self.brdpanel = HexicPanel(panel, 0, brd)
		panelCtl = wx.Panel(panel)
		btnFindPlays = wx.Button(panelCtl, -1, "Find Plays")
		self.Bind(wx.EVT_BUTTON, self.FindPlaysClick, btnFindPlays)

		btnPlayNext = wx.Button(panelCtl, -1, "Play next!")
		self.Bind(wx.EVT_BUTTON, self.PlayNextClick, btnPlayNext)

		sizer = wx.BoxSizer(wx.VERTICAL)
		sizer.Add(self.brdpanel, 0, wx.ALL, 2);
		sizer.Add(panelCtl, 0, wx.ALL, 2);
		panel.SetSizer(sizer)
		panel.Layout()

		sizer = wx.BoxSizer(wx.HORIZONTAL)
		sizer.Add(btnFindPlays, 0, wx.ALL, 2);
		sizer.Add(btnPlayNext, 0, wx.ALL, 2);
		panelCtl.SetSizer(sizer)
		panelCtl.Layout()
	

	def FindPlaysClick(self, evt):
		self.iPlay = 0
		self.brd.RunBoard()
		self.brd.FindPlays(2)
		self.PlayNextClick(evt)

	def PlayNextClick(self, evt):
		if self.iPlay > 0:
			self.brd.RunBoard()
		if (self.iPlay >= self.brd.PlayGetCount()):
			print "End of plays"
			self.brdpanel.Refresh()
			return
		self.brd.Rotate(self.brd.PlayGet(self.iPlay))
		self.brdpanel.Refresh()
		
		self.iPlay += 1



	

		

class TheApp(wx.App):
	def OnInit(self):
		brd = hexicsolve.Board("board.txt")
		frame = HexicSolverFrame(None, "Hexic Solver!", brd)
		self.SetTopWindow(frame)
		frame.Show(True)
		return True

def main():
	app = TheApp()
	app.MainLoop()
	pass


		
	
#--- Main
try:
	main()
finally:
	pass

